package com.xmtlxx.web.controller.center.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.xmtlxx.common.core.domain.entity.SysUser;
import com.xmtlxx.common.enums.PropertyLogType;
import com.xmtlxx.common.exception.ServiceException;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.sms.aliyun.AliyunSmsService;
import com.xmtlxx.common.sms.aliyun.SmsRequest;
import com.xmtlxx.common.sms.aliyun.SmsTemplateCode;
import com.xmtlxx.common.sms.aliyun.SmsTemplateParam;
import com.xmtlxx.common.utils.DateUtils;
import com.xmtlxx.common.utils.JsonUtils;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.file.FileUtils;
import com.xmtlxx.oms.domain.bo.TaskBo;
import com.xmtlxx.oms.domain.vo.PropertyVo;
import com.xmtlxx.oms.domain.vo.StagingVo;
import com.xmtlxx.oms.domain.vo.TaskVo;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.service.ISysOssService;
import com.xmtlxx.system.service.ISysUserService;
import com.xmtlxx.tms.domain.bo.OrderBo;
import com.xmtlxx.tms.domain.bo.OrderDtlBo;
import com.xmtlxx.tms.domain.bo.WaybillBo;
import com.xmtlxx.tms.domain.bo.WaybillDtlBo;
import com.xmtlxx.tms.domain.vo.*;
import com.xmtlxx.tms.service.ICarrierService;
import com.xmtlxx.web.controller.center.service.ICommonService;
import com.xmtlxx.web.controller.center.service.IMobileService;
import com.xmtlxx.web.controller.center.vo.BatchHandover;
import com.xmtlxx.web.controller.oms.service.IPropertyService;
import com.xmtlxx.web.controller.oms.service.IStagingService;
import com.xmtlxx.web.controller.oms.service.ITaskService;
import com.xmtlxx.web.controller.tms.service.IOrderDtlService;
import com.xmtlxx.web.controller.tms.service.IOrderService;
import com.xmtlxx.web.controller.tms.service.IWaybillDtlService;
import com.xmtlxx.web.controller.tms.service.IWaybillService;
import com.xmtlxx.web.controller.wms.service.*;
import com.xmtlxx.wms.domain.HandoverDtl;
import com.xmtlxx.wms.domain.bo.*;
import com.xmtlxx.wms.domain.vo.*;
import com.xmtlxx.wms.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class MobileServiceImpl implements IMobileService {

    private final ISysOssService ossService;

    private final IHandoverService handoverService;

    private final IHandoverDtlService handoverDtlService;

    private final IOrderService orderService;

    private final IOrderDtlService orderDtlService;

    private final IWaybillService waybillService;

    private final IWaybillDtlService waybillDtlService;

    private final IWarehouseService warehouseService;

    private final IWarehouseDtlService warehouseDtlService;

    private final IExWarehouseService exWarehouseService;

    private final IExWarehouseDtlService exWarehouseDtlService;

    private final ISysUserService userService;

    private final ICarrierService carrierService;

    private final ITaskService taskService;

    private final ICommonService commonService;

    private final IStoreService storeService;

    private final IStagingService stagingService;

    private final IInventoryService inventoryService;

    private final IPropertyService propertyService;

    private final IPutDtlService putDtlService;

    private final IDealService dealService;

    private final AliyunSmsService aliyunSmsService;

    /**
     * 附件上传
     *
     * @param file
     * @param lng
     * @param lat
     * @param addr
     * @return
     * @throws IOException
     */

    @Override
    public Map<String, String> fileUpload(MultipartFile file, String lng, String lat, String addr) throws IOException {

        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        Map<String, Object> params = new HashMap<>();
//        lng = "118.998989";
//        lat = "24.99876878";
//        addr = "福建省厦门市集美区杏林街道宁宝花园文达路21号4梯507室";
        if (StringUtils.isNotBlank(lng) && StringUtils.isNotBlank(lat) && StringUtils.isNotBlank(addr)) {
//            String text = "陈荣瑜_01:54_2022.06.06_福建省厦门市集美区杏林街道宁宝花园文达路21号4梯507室_118.998989 24.99876878";
            String text = LoginHelper.getNickname() + "_"
                    + DateUtils.date2String(new Date(), "HH:mm") + "_"
                    + DateUtils.date2String(new Date(), "yyyy-MM-dd") + "_"
                    + addr + "_"
                    + lng + " " + lat;
            params.put("mark", 1);
            params.put("text", text);
        }


        File newImage = FileUtils.MultipartFileToFile(file);
        String suffix = newImage.getPath().substring(newImage.getPath().lastIndexOf(".") + 1);
        Thumbnails.Builder<File> thumbnails = Thumbnails.of(newImage).scale(1).rotate(-90);
        thumbnails.outputFormat(suffix).allowOverwrite(true).toFile(newImage);

        MultipartFile routerFile = FileUtils.fileToMultipartFile(newImage);
        SysOss tOss = ossService.upload(routerFile, params);

        // 设定背景色
        BufferedImage image = ImageIO.read(newImage);
        BufferedImage result = new BufferedImage(
                image.getWidth(),
                image.getHeight(),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D graphic = result.createGraphics();
        graphic.drawImage(image, 0, 0, Color.WHITE, null);
        graphic.dispose();
        ImageIO.write(result, "png", new File(newImage.getPath()));

        MultipartFile multipartFile = FileUtils.fileToMultipartFile(newImage);
        SysOss oss = ossService.upload(multipartFile, params);


        Map<String, String> map = new HashMap<>(2);
        map.put("url", oss.getUrl());
        map.put("fileName", oss.getFileName());
        map.put("ossId", oss.getOssId().toString());
        map.put("tOssId", tOss.getOssId().toString());
        return map;
    }

    /**
     * 交接初始化
     *
     * @param id
     * @param action
     * @return
     */
    @Override
    public HandoverVo handoverBean(String id, String action) {

        HandoverVo vo = new HandoverVo();
        List<HandoverDtlVo> dtls = new ArrayList<>();

        OrderVo orderVo = null;
        WaybillVo waybillVo;
        WaybillDtlBo wdtlBo;
        List<WaybillDtlVo> wDtlVos;
        WarehouseVo warehouseVo;
        ExWarehouseVo exWarehouseVo;
        CarrierVo carrierVo;

        switch (action) {
            case "0"://取件交接;
                /**
                 * 订单列表；取件
                 */
                orderVo = orderService.queryById(id);

                OrderDtlBo orderDtlBo = new OrderDtlBo();
                orderDtlBo.setOrderId(id);
                List<OrderDtlVo> orderDtlVos = orderDtlService.queryList(orderDtlBo);
                dtls = orderDtlVos.stream().map(item -> {
                    HandoverDtlVo dtl = new HandoverDtlVo();
                    BeanUtil.copyProperties(item, dtl, "id");
                    dtl.setOss(new ArrayList<>());
                    return dtl;
                }).collect(Collectors.toList());

                vo.setObjid(orderVo.getTaskId());

                vo.setType("0");//交接类型 0-取件 1-签收 2-暂存

                /**
                 * 取件时间
                 */
                vo.setPickUpTime(new Date());

                switch (orderVo.getType()) {
                    case "0"://入库取件
                        //交货人
                        vo.setOperator(orderVo.getDeptId() != null ? userService.getDeptUser(orderVo.getDeptId()).getUserId() : null);
                        vo.setOperatorName(orderVo.getDeptId() != null ? userService.getDeptUser(orderVo.getDeptId()).getNickName() : null);
                        //接货人
                        vo.setCounterpart(LoginHelper.getUserId());
                        vo.setCounterpartName(LoginHelper.getNickname());
                        break;
                    case "1"://出库取件
                        //交货人
                        vo.setCounterpart(orderVo.getDeptId() != null ? userService.getDeptUser(orderVo.getDeptId()).getUserId() : null);
                        vo.setCounterpartName(orderVo.getDeptId() != null ? userService.getDeptUser(orderVo.getDeptId()).getNickName() : null);
                        //接货人
                        vo.setOperator(LoginHelper.getUserId());
                        vo.setOperatorName(LoginHelper.getNickname());
                        break;
                }

                vo.setSendContacts(orderVo.getSendContacts());
                vo.setSendTel(orderVo.getSendTel());

                break;
            case "1"://收件交接
                /**
                 * WMS签收列表：签收
                 */
                waybillVo = waybillService.queryById(id);
                wdtlBo = new WaybillDtlBo();
                wdtlBo.setWaybillId(waybillVo.getId());
                wDtlVos = waybillDtlService.queryList(wdtlBo);

                dtls = wDtlVos.stream().map(item -> {
                    HandoverDtlVo dtl = new HandoverDtlVo();
                    BeanUtil.copyProperties(item, dtl, "id");
                    dtl.setOss(new ArrayList<>());
                    return dtl;
                }).collect(Collectors.toList());

                orderVo = orderService.queryById(waybillVo.getOrderId());

                /**
                 * 非暂存收件
                 */
                if (StringUtils.isNotEmpty(orderVo.getTaskId())) {
                    HandoverBo bo = new HandoverBo();
                    bo.setObjid(orderVo.getTaskId());
                    bo.setType("0");
                    HandoverVo pickHandover = handoverService.queryByObjid(bo);
                    if (pickHandover != null) {
                        vo.setSigns(pickHandover.getSigns());//移送人签名
                        vo.setHandPics(pickHandover.getHandPics());//取货人签名
                    }
                    vo.setTaskId(orderVo.getTaskId());

                    HandoverDtlBo dtlBo = new HandoverDtlBo();
                    dtlBo.setHid(pickHandover.getId());
                    dtls = handoverDtlService.queryList(dtlBo);
                    dtls = dtls.stream().map(item -> {
                        HandoverDtlVo dtl = new HandoverDtlVo();
                        BeanUtil.copyProperties(item, dtl);
                        dtl.setOss(new ArrayList<>());
                        return dtl;
                    }).collect(Collectors.toList());

                }

                vo.setType("1");//交接类型 0-取件 1-签收 2-暂存

                switch (orderVo.getType()) {//0-入库 1-出库 2-处置 3-调用
                    case "0":
                        //交货人
                        vo.setOperator(orderVo.getDispatchId());
                        vo.setOperatorName(userService.selectUserById(orderVo.getDispatchId()).getNickName());
                        //接货人
                        vo.setCounterpart(LoginHelper.getUserId());
                        vo.setCounterpartName(LoginHelper.getNickname());
                        break;
                    case "4"://暂存
                        //交货人
                        vo.setOperator(orderVo.getDispatchId());
                        vo.setOperatorName(userService.selectUserById(orderVo.getDispatchId()).getNickName());
                        //接货人
                        vo.setCounterpart(LoginHelper.getUserId());
                        vo.setCounterpartName(LoginHelper.getNickname());
                        break;
                    default:
                        //交货人
                        vo.setCounterpart(orderVo.getPickupId());
                        vo.setCounterpartName(userService.selectUserById(orderVo.getPickupId()).getNickName());
                        //接货人
                        vo.setOperator(LoginHelper.getUserId());
                        vo.setOperatorName(LoginHelper.getNickname());
                        break;
                }

                vo.setArrivalTime(orderVo.getArriveTime());
                vo.setTel(orderVo.getSendTel());

                vo.setObjid(StringUtils.isNotEmpty(orderVo.getTaskId()) ? orderVo.getTaskId() : orderVo.getId());

                break;
            case "2"://出库签收交接
                /**
                 * 出库单列表：出库单签收
                 */
                exWarehouseVo = exWarehouseService.queryById(id);

                ExWarehouseDtlBo eWDtlBo = new ExWarehouseDtlBo();
                eWDtlBo.setWid(exWarehouseVo.getId());
                List<ExWarehouseDtlVo> eWDtlVos = exWarehouseDtlService.queryList(eWDtlBo);

                dtls = eWDtlVos.stream().map(item -> {
                    HandoverDtlVo dtl = new HandoverDtlVo();
                    BeanUtil.copyProperties(item, dtl, "id");
                    dtl.setOss(new ArrayList<>());
                    return dtl;
                }).collect(Collectors.toList());

                vo.setType("1");//交接类型 0-取件 1-签收 2-暂存

                //接货人
                vo.setCounterpart(exWarehouseVo.getTargetId() != null ? userService.getDeptUser(exWarehouseVo.getTargetId()).getUserId() : null);
                vo.setCounterpartName(exWarehouseVo.getTargetId() != null ? userService.getDeptUser(exWarehouseVo.getTargetId()).getNickName() : null);

                //交货人
                vo.setOperator(LoginHelper.getUserId());
                vo.setOperatorName(LoginHelper.getNickname());

                vo.setObjid(StringUtils.isNotEmpty(exWarehouseVo.getTaskId()) ? exWarehouseVo.getTaskId() : exWarehouseVo.getId());

                break;
            case "3"://入库签收交接
                /**
                 * OMS签收列表：到货
                 */
                warehouseVo = warehouseService.queryById(id);

                WarehouseDtlBo whDtlBo = new WarehouseDtlBo();
                whDtlBo.setWid(warehouseVo.getId());
                List<WarehouseDtlVo> whDtlVos = warehouseDtlService.queryList(whDtlBo);

                dtls = whDtlVos.stream().map(item -> {
                    HandoverDtlVo dtl = new HandoverDtlVo();
                    BeanUtil.copyProperties(item, dtl, "id");
                    dtl.setOss(new ArrayList<>());
                    return dtl;
                }).collect(Collectors.toList());

                vo.setType("1");//交接类型 0-取件 1-签收 2-暂存

                //交货人
                vo.setOperator(warehouseVo.getDeptId() != null ? userService.getDeptUser(warehouseVo.getDeptId()).getUserId() : null);
                vo.setOperatorName(warehouseVo.getDeptId() != null ? userService.getDeptUser(warehouseVo.getDeptId()).getNickName() : null);

                //接货人
                vo.setCounterpart(LoginHelper.getUserId());
                vo.setCounterpartName(LoginHelper.getNickname());

                vo.setObjid(StringUtils.isNotEmpty(warehouseVo.getTaskId()) ? warehouseVo.getTaskId() : warehouseVo.getId());

                /**
                 * 交接清单共用参数
                 * 移送单位 transferDeptName
                 * 订单编号 taskNo
                 * 取货时间 handTime
                 * 取货人 counterpartName
                 * 取货地点 pickUpAddress
                 * 到货时间 arrivalTime
                 * 到货人 arrivaler
                 * 到货地点 arrivalAddress
                 */
                vo.setTransferDeptName(warehouseVo.getDeptName());
                vo.setTaskNo(warehouseVo.getWno());
                vo.setArrivalAddress(warehouseVo.getAddress());

                break;
            case "4"://暂存交接
                /**
                 * 运单列表：暂存
                 */
                waybillVo = waybillService.queryById(id);
                wdtlBo = new WaybillDtlBo();
                wdtlBo.setWaybillId(waybillVo.getId());
                wDtlVos = waybillDtlService.queryList(wdtlBo);

                dtls = wDtlVos.stream().map(item -> {
                    HandoverDtlVo dtl = new HandoverDtlVo();
                    BeanUtil.copyProperties(item, dtl, "id");
                    dtl.setOss(new ArrayList<>());
                    return dtl;
                }).collect(Collectors.toList());

                orderVo = orderService.queryById(waybillVo.getOrderId());

                vo.setType("2");//交接类型 0-取件 1-签收 2-暂存

                switch (orderVo.getType()) {
                    case "0":
                        //交货人
                        vo.setOperator(orderVo.getDispatchId());
                        vo.setOperatorName(userService.selectUserById(orderVo.getDispatchId()).getNickName());
                        //暂存承运商
                        carrierVo = carrierService.queryById(waybillVo.getCarrierId());
                        if (carrierVo != null) {
                            vo.setCounterpart(carrierVo.getBingUserId());
                            vo.setCounterpartName(carrierVo.getNickName());
                        }
                        break;
                    default:
                        //暂存承运商
                        carrierVo = carrierService.queryById(waybillVo.getCarrierId());
                        if (carrierVo != null) {
                            vo.setCounterpart(carrierVo.getBingUserId());
                            vo.setCounterpartName(carrierVo.getNickName());
                        }
                        //接货人
                        vo.setOperator(LoginHelper.getUserId());
                        vo.setOperatorName(LoginHelper.getNickname());
                        break;
                }

                /**
                 * 非暂存收件
                 */
                if (StringUtils.isNotEmpty(orderVo.getTaskId())) {
                    HandoverBo bo = new HandoverBo();
                    bo.setObjid(orderVo.getTaskId());
                    bo.setType("0");
                    HandoverVo pickHandover = handoverService.queryByObjid(bo);
                    if (pickHandover != null) {
                        vo.setSigns(pickHandover.getSigns());//移送人签名
                        vo.setHandPics(pickHandover.getHandPics());//取货人签名
                    }
                    vo.setTaskId(orderVo.getTaskId());

                    HandoverDtlBo dtlBo = new HandoverDtlBo();
                    dtlBo.setHid(pickHandover.getId());
                    dtls = handoverDtlService.queryList(dtlBo);

                }
                vo.setObjid(StringUtils.isNotEmpty(orderVo.getTaskId()) ? orderVo.getTaskId() : orderVo.getId());
                break;
        }
        vo.setItems(dtls);
        vo.setOss(new ArrayList<>());
        vo.setSignsStatus("0");

        /**
         * 交接清单共用参数
         * 移送单位 transferDeptName
         * 订单编号 taskNo
         * 取货时间 handTime
         * 取货人 counterpartName
         * 取货地点 pickUpAddress
         * 到货时间 arrivalTime
         * 到货人 arrivaler
         * 到货地点 arrivalAddress
         */
        if (ObjectUtil.isNotNull(orderVo)) {
            vo.setTransferDeptName(orderVo.getDeptName());
            vo.setTaskNo(orderVo.getOrderNo());
            vo.setArrivaler(orderVo.getTakeContacts());
            vo.setPickUpAddress(orderVo.getSendAddress());
            vo.setArrivalAddress(orderVo.getTakeAddress());
        }

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HandoverVo handoverBeanVo(String id, String action) {

        HandoverVo vo = new HandoverVo();
        List<HandoverDtlVo> dtls = new ArrayList<>();
        List<SysOss> carSoss = new ArrayList<>();
        HandoverBo bo = new HandoverBo();
        HandoverVo old = new HandoverVo();

        List<SysOss> facadeOss = new ArrayList<>();

        TaskVo taskVo = null;
        StoreVo storeVo = null;
        OrderVo orderVo = null;
        WaybillVo waybillVo;
        WaybillDtlBo wdtlBo;
        List<WaybillDtlVo> wdtls;
        WarehouseVo warehouseVo;
        ExWarehouseVo exWarehouseVo;
        CarrierVo carrierVo;
        SysUser storeUser = null;

        SysOssBo orderOssBo = new SysOssBo();
        SysUser userBo = new SysUser();
        List<SysUser> users = new ArrayList<>();

        /**
         * 交接清单共用参数
         * 移送单位 transferDeptName
         * 订单编号 taskNo
         * 取货时间 handTime
         * 取货人 counterpartName
         * 取货地点 pickUpAddress
         * 到货时间 arrivalTime
         * 到货人 arrivaler
         * 到货地点 arrivalAddress
         * 联系人 sendContacts
         * 联系电话 sendTel
         */
        String transferDeptName = null;
        String taskNo = null;
        Date pickUpTime = null;
        String counterpartName = null;
        String pickUpAddress = null;
        Date arrivalTime = null;
        String arrivaler = null;
        String arrivalAddress = null;
        String sendContacts = null;
        String sendTel = null;


        /**
         * action 0-取件 1-签收 2-出库签收交接 3-入库签收交接 4-暂存签收交接
         */
        switch (action) {
            case "0":
                orderVo = orderService.queryById(id);
                taskVo = taskService.queryById(orderVo.getTaskId());
                storeVo = storeService.queryById(taskVo.getStoreId());
                userBo.setDeptId(storeVo.getDeptId());
                userBo.setUserName("smscg");
                users = userService.selectUsers(userBo);
                if (users != null && users.size() > 0) {
                    storeUser = users.get(0);
                }
                //当前为车辆单时，判断是否上传必要附件

                if (orderVo.getBuss().equals("1")) {
                    orderOssBo.setObjid(orderVo.getId());

                    //全车外观
                    orderOssBo.setBuss("facade");
                    facadeOss = ossService.selectSysOssList(orderOssBo);
                    if (facadeOss == null || facadeOss.size() == 0) {
                        vo.setCode("101");
                        return vo;
                    }

                    orderOssBo.setBuss("carNumber");
                    List<SysOss> carOss = ossService.selectSysOssList(orderOssBo);
                    if (carOss == null || carOss.size() == 0) {
                        vo.setCode("101");
                        return vo;
                    } else {
                        carSoss.addAll(carOss);
                    }

                    orderOssBo.setBuss("handover");
                    List<SysOss> handoverOss = ossService.selectSysOssList(orderOssBo);
                    if (handoverOss == null || handoverOss.size() == 0) {
                        vo.setCode("101");
                        return vo;
                    } else {
                        carSoss.addAll(handoverOss);
                    }

//                    orderOssBo.setBuss("load");
//                    List<SysOss> loadOss = ossService.selectSysOssList(orderOssBo);
//                    if(loadOss == null || loadOss.size() == 0){
//                        vo.setCode("101");
//                        return vo;
//                    }else {
//                        carSoss.addAll(loadOss);
//                    }
                }


                OrderDtlBo otlBo = new OrderDtlBo();
                otlBo.setOrderId(id);
                List<OrderDtlVo> odtls = orderDtlService.queryList(otlBo);

                bo.setObjid(orderVo.getTaskId());
                bo.setType("0");
                old = handoverService.queryByObjid(bo);

                if (old == null) {
                    //0-入库 其余全出库
                    switch (orderVo.getType()) {
                        case "0":
                            //移送人
                            bo.setOperator(orderVo.getDeptId() != null ? userService.getDeptUser(orderVo.getDeptId()).getUserId() : null);
                            //取件人
                            bo.setCounterpart(LoginHelper.getUserId());

                            /**
                             * 交接清单共用参数
                             * 移送单位 transferDeptName
                             * 订单编号 taskNo
                             * 取货时间 pickUpTime
                             * 取货人 counterpartName
                             * 取货地点 pickUpAddress
                             * 到货时间 arrivalTime
                             * 到货人 arrivaler
                             * 到货地点 arrivalAddress
                             * 联系人 sendContacts
                             * 联系电话 sendTel
                             */
                            transferDeptName = orderVo.getDeptName();
                            taskNo = orderVo.getOrderNo();
                            pickUpTime = new Date();
                            counterpartName = LoginHelper.getNickname();
                            pickUpAddress = orderVo.getSendAddress();
                            arrivalTime = null;
                            arrivaler = storeUser.getNickName();
                            arrivalAddress = storeVo.getAddress();
                            sendContacts = orderVo.getSendContacts();
                            sendTel = orderVo.getSendTel();

                            break;
                        default:
                            //移送人
                            bo.setOperator(storeUser.getUserId());//仓管
                            //取件人
                            bo.setCounterpart(LoginHelper.getUserId());

                            /**
                             * 交接清单共用参数
                             * 移送单位 transferDeptName
                             * 订单编号 taskNo
                             * 取货时间 pickUpTime
                             * 取货人 counterpartName
                             * 取货地点 pickUpAddress
                             * 到货时间 arrivalTime
                             * 到货人 arrivaler
                             * 到货地点 arrivalAddress
                             * 联系人 sendContacts
                             * 联系电话 sendTel
                             */
                            transferDeptName = storeVo.getDeptName();
                            taskNo = orderVo.getOrderNo();
                            pickUpTime = new Date();
                            counterpartName = LoginHelper.getNickname();
                            pickUpAddress = storeVo.getAddress();
                            arrivalTime = null;
                            arrivaler = orderVo.getTakeContacts();
                            arrivalAddress = orderVo.getTakeAddress();
                            sendContacts = storeUser.getNickName();
                            sendTel = storeVo.getTel();
                            break;
                    }
                    List<SysOss> finalFacadeOss = facadeOss;
                    OrderVo finalOrderVo = orderVo;
                    dtls = odtls.stream().map(item -> {
                        HandoverDtlVo dtl = new HandoverDtlVo();
                        BeanUtil.copyProperties(item, dtl, "id");

                        if (finalOrderVo.getBuss().equals("1")) {
                            dtl.setOss(finalFacadeOss);
                        } else {
                            dtl.setOss(new ArrayList<>());
                        }


                        /**
                         * 获取接收登记图片
                         */
                        StagingVo stagingVo = stagingService.getVoByNo(dtl.getPno());
                        List<SysOss> soss = new ArrayList<>();
                        if (stagingVo != null) {
                            SysOssBo ossBo = new SysOssBo();
                            ossBo.setObjid(stagingVo.getId());
                            ossBo.setBuss("staging");
//                        dtl.setSOss(ossService.selectSysOssList(ossBo));
                            dtl.setFeature(stagingVo.getFeature());
                            soss.addAll(ossService.selectSysOssList(ossBo));
                        }
                        if (carSoss != null && carSoss.size() > 0) {
                            soss.addAll(carSoss);
                        }
                        dtl.setSOss(soss);

                        return dtl;
                    }).collect(Collectors.toList());
                    bo.setItems(dtls);
                    bo.setSignsStatus("0");

                    vo = handoverService.insertByBo(bo);
                } else {
                    BeanUtil.copyProperties(old, vo);

                    HandoverDtlBo dtlBo = new HandoverDtlBo();
                    dtlBo.setHid(vo.getId());
                    dtls = handoverDtlService.queryList(dtlBo);

                    dtls.stream().forEach(item -> {

                        SysOssBo ossBo = new SysOssBo();
                        ossBo.setObjid(item.getId());
                        ossBo.setBuss("handoverDtl");
                        List<SysOss> ossList = ossService.selectSysOssList(ossBo);
                        item.setOss(ossList);

                        /**
                         * 获取接收登记图片
                         */
                        StagingVo stagingVo = stagingService.getVoByNo(item.getPno());
                        List<SysOss> soss = new ArrayList<>();
                        if (stagingVo != null) {
                            SysOssBo sossBo = new SysOssBo();
                            sossBo.setObjid(stagingVo.getId());
                            sossBo.setBuss("staging");

                            soss.addAll(ossService.selectSysOssList(sossBo));
                            item.setFeature(stagingVo.getFeature());
                        }
                        if (carSoss != null && carSoss.size() > 0) {
                            soss.addAll(carSoss);
                        }
                        item.setSOss(soss);

//                        item.setSOss(ossService.selectSysOssList(sossBo));

                    });
                    vo.setItems(dtls);

                    //0-入库 其余全出库
                    switch (orderVo.getType()) {
                        case "0":
                            /**
                             * 交接清单共用参数
                             * 移送单位 transferDeptName
                             * 订单编号 taskNo
                             * 取货时间 pickUpTime
                             * 取货人 counterpartName
                             * 取货地点 pickUpAddress
                             * 到货时间 arrivalTime
                             * 到货人 arrivaler
                             * 到货地点 arrivalAddress
                             * 联系人 sendContacts
                             * 联系电话 sendTel
                             */
                            transferDeptName = orderVo.getDeptName();
                            taskNo = orderVo.getOrderNo();
                            pickUpTime = new Date();
                            counterpartName = LoginHelper.getNickname();
                            pickUpAddress = orderVo.getSendAddress();
                            arrivalTime = null;
                            arrivaler = storeUser.getNickName();
                            arrivalAddress = storeVo.getAddress();
                            sendContacts = orderVo.getSendContacts();
                            sendTel = orderVo.getSendTel();
                            break;
                        default:

                            /**
                             * 交接清单共用参数
                             * 移送单位 transferDeptName
                             * 订单编号 taskNo
                             * 取货时间 pickUpTime
                             * 取货人 counterpartName
                             * 取货地点 pickUpAddress
                             * 到货时间 arrivalTime
                             * 到货人 arrivaler
                             * 到货地点 arrivalAddress
                             * 联系人 sendContacts
                             * 联系电话 sendTel
                             */
                            transferDeptName = storeVo.getDeptName();
                            taskNo = orderVo.getOrderNo();
                            pickUpTime = new Date();
                            counterpartName = LoginHelper.getNickname();
                            pickUpAddress = storeVo.getAddress();
                            arrivalTime = null;
                            arrivaler = orderVo.getTakeContacts();
                            arrivalAddress = orderVo.getTakeAddress();
                            sendContacts = storeUser.getNickName();
                            sendTel = storeVo.getTel();
                            break;
                    }
                }

                break;
            case "1":

                waybillVo = waybillService.queryById(id);

                wdtlBo = new WaybillDtlBo();
                wdtlBo.setWaybillId(waybillVo.getId());
                wdtls = waybillDtlService.queryList(wdtlBo);

                orderVo = orderService.queryById(waybillVo.getOrderId());
                taskVo = taskService.queryById(orderVo.getTaskId());
                storeVo = storeService.queryById(taskVo.getStoreId());
                userBo.setDeptId(storeVo.getDeptId());
                userBo.setUserName("smscg");
                users = userService.selectUsers(userBo);
                if (users != null && users.size() > 0) {
                    storeUser = users.get(0);
                }
                if (orderVo.getBuss().equals("1")) {
                    orderOssBo.setObjid(orderVo.getId());

                    //全车外观
                    orderOssBo.setBuss("facade");
                    facadeOss = ossService.selectSysOssList(orderOssBo);

                    orderOssBo.setBuss("carNumber");
                    List<SysOss> carOss = ossService.selectSysOssList(orderOssBo);
                    carSoss.addAll(carOss);

                    orderOssBo.setBuss("handover");
                    List<SysOss> handoverOss = ossService.selectSysOssList(orderOssBo);
                    carSoss.addAll(handoverOss);

                    orderOssBo.setBuss("load");
                    List<SysOss> loadOss = ossService.selectSysOssList(orderOssBo);
                    carSoss.addAll(loadOss);

                    orderOssBo.setBuss("take");
                    List<SysOss> takeOss = ossService.selectSysOssList(orderOssBo);
                    if (takeOss == null || takeOss.size() == 0) {
                        vo.setCode("101");
                        vo.setOrderId(orderVo.getId());
                        return vo;
                    } else {
                        carSoss.addAll(takeOss);
                    }

                    orderOssBo.setBuss("unload");
                    List<SysOss> unloadOss = ossService.selectSysOssList(orderOssBo);
                    if (unloadOss == null || unloadOss.size() == 0) {
                        vo.setCode("101");
                        vo.setOrderId(orderVo.getId());
                        return vo;
                    } else {
                        carSoss.addAll(unloadOss);
                    }

                }

                bo.setObjid(orderVo.getTaskId());
                //交接类型 0-取件 1-签收 2-暂存
                bo.setType("1");
                old = handoverService.queryByObjid(bo);

                if (old == null) {
                    switch (orderVo.getType()) {
                        case "0":
                            //取件人
                            bo.setOperator(orderVo.getPickupId());
                            //中心仓管
                            bo.setCounterpart(LoginHelper.getUserId());

                            /**
                             * 交接清单共用参数
                             * 移送单位 transferDeptName
                             * 订单编号 taskNo
                             * 取货时间 pickUpTime
                             * 取货人 counterpartName
                             * 取货地点 pickUpAddress
                             * 到货时间 arrivalTime
                             * 到货人 arrivaler
                             * 到货地点 arrivalAddress
                             * 联系人 sendContacts
                             * 联系电话 sendTel
                             */
                            transferDeptName = orderVo.getDeptName();
                            taskNo = orderVo.getOrderNo();
//                            pickUpTime = new Date();
//                            counterpartName = LoginHelper.getNickname();
                            pickUpAddress = orderVo.getSendAddress();
                            arrivalTime = new Date();
                            arrivaler = LoginHelper.getNickname();
                            arrivalAddress = storeVo.getAddress();
                            sendContacts = orderVo.getSendContacts();
                            sendTel = orderVo.getSendTel();

                            break;
                        default:
                            //取件人
                            bo.setOperator(orderVo.getPickupId());
                            //收件人
                            bo.setCounterpart(LoginHelper.getUserId());

                            /**
                             * 交接清单共用参数
                             * 移送单位 transferDeptName
                             * 订单编号 taskNo
                             * 取货时间 pickUpTime
                             * 取货人 counterpartName
                             * 取货地点 pickUpAddress
                             * 到货时间 arrivalTime
                             * 到货人 arrivaler
                             * 到货地点 arrivalAddress
                             * 联系人 sendContacts
                             * 联系电话 sendTel
                             */
                            transferDeptName = storeVo.getDeptName();
                            taskNo = orderVo.getOrderNo();
//                            pickUpTime = new Date();
//                            counterpartName = LoginHelper.getNickname();
                            pickUpAddress = storeVo.getAddress();
                            arrivalTime = new Date();
                            arrivaler = orderVo.getTakeContacts();
                            arrivalAddress = orderVo.getTakeAddress();
                            sendContacts = storeUser.getNickName();
                            sendTel = storeVo.getTel();
                            break;
                    }

                    /**
                     * 非暂存收件
                     */
                    if (StringUtils.isNotEmpty(orderVo.getTaskId())) {
                        /**
                         * 获取取件下的移送人以及取件人签名信息
                         */
                        HandoverBo pickbo = new HandoverBo();
                        pickbo.setObjid(orderVo.getTaskId());
                        pickbo.setType("0");
                        HandoverVo pickHandover = handoverService.queryByObjid(pickbo);
                        if (pickHandover != null) {
                            if (taskVo.getType().equals("0")) {
                                bo.setSigns(pickHandover.getSigns());//移送人签名
                                bo.setHandPics(pickHandover.getHandPics());//取货人签名
                                pickUpTime = pickHandover.getHandTime();//取件时间
                                counterpartName = pickHandover.getCounterpartName();//取货人
                            } else {
                                bo.setCounterpartSign(pickHandover.getCounterpartSign());//中心受理人签名
                                bo.setCheckSign(pickHandover.getCheckSign());//中心保管人签名
                                bo.setHandPics(pickHandover.getHandPics());//送件人签名
                                pickUpTime = pickHandover.getHandTime();//取件时间
                                counterpartName = pickHandover.getCounterpartName();//取货人
                            }
                        }
                        /**
                         * 获取取件时的物品清单
                         */
                        HandoverDtlBo dtlBo = new HandoverDtlBo();
                        dtlBo.setHid(pickHandover.getId());
                        dtls = handoverDtlService.queryList(dtlBo);
                        OrderVo finalOrderVo1 = orderVo;
                        List<SysOss> finalFacadeOss1 = facadeOss;
                        dtls = dtls.stream().map(item -> {
                            HandoverDtlVo dtl = new HandoverDtlVo();
                            BeanUtil.copyProperties(item, dtl);
//                            dtl.setOss(new ArrayList<>());

                            if (finalOrderVo1.getBuss().equals("1")) {
                                dtl.setOss(finalFacadeOss1);
                            } else {
                                dtl.setOss(new ArrayList<>());
                            }

                            /**
                             * 获取接收登记图片
                             */
                            StagingVo stagingVo = stagingService.getVoByNo(dtl.getPno());
                            List<SysOss> soss = new ArrayList<>();
                            if (stagingVo != null) {
                                SysOssBo ossBo = new SysOssBo();
                                ossBo.setObjid(stagingVo.getId());
                                ossBo.setBuss("staging");
//                            dtl.setSOss(ossService.selectSysOssList(ossBo));
                                dtl.setFeature(stagingVo.getFeature());

                                soss.addAll(ossService.selectSysOssList(ossBo));
                            }
                            if (carSoss != null && carSoss.size() > 0) {
                                soss.addAll(carSoss);
                            }
                            dtl.setSOss(soss);

                            return dtl;
                        }).collect(Collectors.toList());

                    }
                    bo.setItems(dtls);
                    bo.setSignsStatus("0");
                    vo = handoverService.insertByBo(bo);
                    vo.setOss(new ArrayList<>());
                    vo.setTaskId(orderVo.getTaskId());
                    vo.setTaskType(taskVo.getType());
                } else {
                    BeanUtil.copyProperties(old, vo);

                    HandoverDtlBo dtlBo = new HandoverDtlBo();
                    dtlBo.setHid(vo.getId());
                    dtls = handoverDtlService.queryList(dtlBo);

                    dtls.stream().forEach(item -> {

                        SysOssBo ossBo = new SysOssBo();
                        ossBo.setObjid(item.getId());
                        ossBo.setBuss("handoverDtl");
                        List<SysOss> ossList = ossService.selectSysOssList(ossBo);
                        item.setOss(ossList);

                        /**
                         * 获取接收登记图片
                         */
                        StagingVo stagingVo = stagingService.getVoByNo(item.getPno());
                        List<SysOss> soss = new ArrayList<>();
                        if (stagingVo != null) {
                            SysOssBo sossBo = new SysOssBo();
                            sossBo.setObjid(stagingVo.getId());
                            sossBo.setBuss("staging");
                            soss.addAll(ossService.selectSysOssList(sossBo));
                        }
                        if (carSoss != null && carSoss.size() > 0) {
                            soss.addAll(carSoss);
                        }
                        item.setSOss(soss);
                        item.setFeature(stagingVo.getFeature());
                    });
                    vo.setItems(dtls);

                    /**
                     * 获取取件下的移送人以及取件人签名信息
                     */
                    HandoverBo pickbo = new HandoverBo();
                    pickbo.setObjid(orderVo.getTaskId());
                    pickbo.setType("0");
                    HandoverVo pickHandover = handoverService.queryByObjid(pickbo);

                    switch (orderVo.getType()) {
                        case "0":
                            /**
                             * 交接清单共用参数
                             * 移送单位 transferDeptName
                             * 订单编号 taskNo
                             * 取货时间 pickUpTime
                             * 取货人 counterpartName
                             * 取货地点 pickUpAddress
                             * 到货时间 arrivalTime
                             * 到货人 arrivaler
                             * 到货地点 arrivalAddress
                             * 联系人 sendContacts
                             * 联系电话 sendTel
                             */
                            transferDeptName = orderVo.getDeptName();
                            taskNo = orderVo.getOrderNo();
                            pickUpTime = pickHandover.getHandTime();
                            counterpartName = pickHandover.getCounterpartName();
                            pickUpAddress = orderVo.getSendAddress();
                            arrivalTime = new Date();
                            arrivaler = LoginHelper.getNickname();
                            arrivalAddress = storeVo.getAddress();
                            sendContacts = orderVo.getSendContacts();
                            sendTel = orderVo.getSendTel();

                            break;
                        default:
                            /**
                             * 交接清单共用参数
                             * 移送单位 transferDeptName
                             * 订单编号 taskNo
                             * 取货时间 pickUpTime
                             * 取货人 counterpartName
                             * 取货地点 pickUpAddress
                             * 到货时间 arrivalTime
                             * 到货人 arrivaler
                             * 到货地点 arrivalAddress
                             * 联系人 sendContacts
                             * 联系电话 sendTel
                             */
                            transferDeptName = storeVo.getDeptName();
                            taskNo = orderVo.getOrderNo();
                            pickUpTime = pickHandover.getHandTime();
                            counterpartName = pickHandover.getCounterpartName();
                            pickUpAddress = storeVo.getAddress();
                            arrivalTime = new Date();
                            arrivaler = orderVo.getTakeContacts();
                            arrivalAddress = orderVo.getTakeAddress();
                            sendContacts = storeUser.getNickName();
                            sendTel = storeVo.getTel();
                            break;
                    }
                }
                vo.setObjid(StringUtils.isNotEmpty(orderVo.getTaskId()) ? orderVo.getTaskId() : orderVo.getId());
                vo.setTaskId(orderVo.getTaskId());
                vo.setTaskType(taskVo.getType());
                break;
            case "2":
                exWarehouseVo = exWarehouseService.queryById(id);
                taskVo = taskService.queryById(exWarehouseVo.getTaskId());
                storeVo = storeService.queryById(taskVo.getStoreId());
                userBo.setDeptId(storeVo.getDeptId());
                userBo.setUserName("smscg");
                users = userService.selectUsers(userBo);
                if (users != null && users.size() > 0) {
                    storeUser = users.get(0);
                }
                ExWarehouseDtlBo eWDtlBo = new ExWarehouseDtlBo();
                eWDtlBo.setWid(exWarehouseVo.getId());
                List<ExWarehouseDtlVo> ewdtls = exWarehouseDtlService.queryList(eWDtlBo);
                bo.setObjid(exWarehouseVo.getTaskId());
                //交接类型 0-取件 1-签收 2-暂存
                bo.setType("1");
                old = handoverService.queryByObjid(bo);

                if (old == null) {
                    //取件人
                    bo.setOperator(storeUser.getUserId());
                    //收件人
                    bo.setCounterpart(LoginHelper.getUserId());

                    /**
                     * 交接清单共用参数
                     * 移送单位 transferDeptName
                     * 订单编号 taskNo
                     * 取货时间 pickUpTime
                     * 取货人 counterpartName
                     * 取货地点 pickUpAddress
                     * 到货时间 arrivalTime
                     * 到货人 arrivaler
                     * 到货地点 arrivalAddress
                     * 联系人 sendContacts
                     * 联系电话 sendTel
                     */
                    transferDeptName = storeVo.getDeptName();
                    taskNo = exWarehouseVo.getExno();
                    pickUpTime = new Date();
                    counterpartName = storeUser.getNickName();
                    pickUpAddress = storeVo.getAddress();
                    arrivalTime = new Date();
                    arrivaler = LoginHelper.getNickname();
                    arrivalAddress = exWarehouseVo.getAddress();
                    sendContacts = storeUser.getNickName();
                    sendTel = storeVo.getTel();

                    dtls = ewdtls.stream().map(item -> {
                        HandoverDtlVo dtl = new HandoverDtlVo();
                        BeanUtil.copyProperties(item, dtl, "id");
                        dtl.setOss(new ArrayList<>());

                        /**
                         * 获取接收登记图片
                         */
                        StagingVo stagingVo = stagingService.getVoByNo(dtl.getPno());

                        if (stagingVo != null) {
                            SysOssBo ossBo = new SysOssBo();
                            ossBo.setObjid(stagingVo.getId());
                            ossBo.setBuss("staging");
                            dtl.setSOss(ossService.selectSysOssList(ossBo));
                            dtl.setFeature(stagingVo.getFeature());
                        }
                        return dtl;
                    }).collect(Collectors.toList());
                    bo.setItems(dtls);
                    bo.setSignsStatus("0");

                    vo = handoverService.insertByBo(bo);
                    vo.setTaskId(bo.getObjid());
                } else {
                    BeanUtil.copyProperties(old, vo);

                    HandoverDtlBo dtlBo = new HandoverDtlBo();
                    dtlBo.setHid(vo.getId());
                    dtls = handoverDtlService.queryList(dtlBo);

                    dtls.stream().forEach(item -> {

                        SysOssBo ossBo = new SysOssBo();
                        ossBo.setObjid(item.getId());
                        ossBo.setBuss("handoverDtl");
                        List<SysOss> ossList = ossService.selectSysOssList(ossBo);
                        item.setOss(ossList);

                        /**
                         * 获取接收登记图片
                         */
                        StagingVo stagingVo = stagingService.getVoByNo(item.getPno());

                        if (stagingVo != null) {
                            SysOssBo sossBo = new SysOssBo();
                            sossBo.setObjid(stagingVo.getId());
                            sossBo.setBuss("staging");
                            item.setSOss(ossService.selectSysOssList(sossBo));
                            item.setFeature(stagingVo.getFeature());
                        }
                    });
                    vo.setItems(dtls);

                    /**
                     * 交接清单共用参数
                     * 移送单位 transferDeptName
                     * 订单编号 taskNo
                     * 取货时间 pickUpTime
                     * 取货人 counterpartName
                     * 取货地点 pickUpAddress
                     * 到货时间 arrivalTime
                     * 到货人 arrivaler
                     * 到货地点 arrivalAddress
                     * 联系人 sendContacts
                     * 联系电话 sendTel
                     */
                    transferDeptName = storeVo.getDeptName();
                    taskNo = exWarehouseVo.getExno();
                    pickUpTime = new Date();
                    counterpartName = storeUser.getNickName();
                    pickUpAddress = storeVo.getAddress();
                    arrivalTime = new Date();
                    arrivaler = LoginHelper.getNickname();
                    arrivalAddress = exWarehouseVo.getAddress();
                    sendContacts = storeUser.getNickName();
                    sendTel = storeVo.getTel();
                }
                vo.setTaskId(bo.getObjid());
                break;
            case "3":
                warehouseVo = warehouseService.queryById(id);
                taskVo = taskService.queryById(warehouseVo.getTaskId());
                storeVo = storeService.queryById(taskVo.getStoreId());
                WarehouseDtlBo whDtlBo = new WarehouseDtlBo();
                whDtlBo.setWid(warehouseVo.getId());
                List<WarehouseDtlVo> whdtls = warehouseDtlService.queryList(whDtlBo);

                bo.setObjid(warehouseVo.getTaskId());
                //交接类型 0-取件 1-签收 2-暂存
                bo.setType("1");
                old = handoverService.queryByObjid(bo);

                if (old == null) {
                    //移送人
                    bo.setOperator(warehouseVo.getDeptId() != null ? userService.getDeptUser(warehouseVo.getDeptId()).getUserId() : null);
                    //中心仓管
                    bo.setCounterpart(LoginHelper.getUserId());

                    /**
                     * 交接清单共用参数
                     * 移送单位 transferDeptName
                     * 订单编号 taskNo
                     * 取货时间 pickUpTime
                     * 取货人 counterpartName
                     * 取货地点 pickUpAddress
                     * 到货时间 arrivalTime
                     * 到货人 arrivaler
                     * 到货地点 arrivalAddress
                     * 联系人 sendContacts
                     * 联系电话 sendTel
                     */
                    transferDeptName = warehouseVo.getDeptName();
                    taskNo = warehouseVo.getWno();
                    pickUpTime = new Date();
                    counterpartName = warehouseVo.getName();
                    pickUpAddress = warehouseVo.getAddress();
                    arrivalTime = new Date();
                    arrivaler = LoginHelper.getNickname();
                    arrivalAddress = storeVo.getAddress();
                    sendContacts = warehouseVo.getName();
                    sendTel = warehouseVo.getTel();


                    dtls = whdtls.stream().map(item -> {
                        HandoverDtlVo dtl = new HandoverDtlVo();
                        BeanUtil.copyProperties(item, dtl, "id");
                        dtl.setOss(new ArrayList<>());

                        /**
                         * 获取接收登记图片
                         */
                        StagingVo stagingVo = stagingService.getVoByNo(dtl.getPno());

                        if (stagingVo != null) {
                            SysOssBo ossBo = new SysOssBo();
                            ossBo.setObjid(stagingVo.getId());
                            ossBo.setBuss("staging");
                            dtl.setSOss(ossService.selectSysOssList(ossBo));
                            dtl.setFeature(stagingVo.getFeature());
                        }
                        return dtl;
                    }).collect(Collectors.toList());
                    bo.setItems(dtls);
                    bo.setSignsStatus("0");

                    vo = handoverService.insertByBo(bo);
                } else {
                    BeanUtil.copyProperties(old, vo);

                    HandoverDtlBo dtlBo = new HandoverDtlBo();
                    dtlBo.setHid(vo.getId());
                    dtls = handoverDtlService.queryList(dtlBo);

                    dtls.stream().forEach(item -> {

                        SysOssBo ossBo = new SysOssBo();
                        ossBo.setObjid(item.getId());
                        ossBo.setBuss("handoverDtl");
                        List<SysOss> ossList = ossService.selectSysOssList(ossBo);
                        item.setOss(ossList);

                        /**
                         * 获取接收登记图片
                         */
                        StagingVo stagingVo = stagingService.getVoByNo(item.getPno());

                        if (stagingVo != null) {
                            SysOssBo sossBo = new SysOssBo();
                            sossBo.setObjid(stagingVo.getId());
                            sossBo.setBuss("staging");
                            item.setSOss(ossService.selectSysOssList(sossBo));
                            item.setFeature(stagingVo.getFeature());
                        }
                    });
                    vo.setItems(dtls);

                    /**
                     * 交接清单共用参数
                     * 移送单位 transferDeptName
                     * 订单编号 taskNo
                     * 取货时间 pickUpTime
                     * 取货人 counterpartName
                     * 取货地点 pickUpAddress
                     * 到货时间 arrivalTime
                     * 到货人 arrivaler
                     * 到货地点 arrivalAddress
                     * 联系人 sendContacts
                     * 联系电话 sendTel
                     */
                    transferDeptName = warehouseVo.getDeptName();
                    taskNo = warehouseVo.getWno();
                    pickUpTime = new Date();
                    counterpartName = warehouseVo.getName();
                    pickUpAddress = warehouseVo.getAddress();
                    arrivalTime = new Date();
                    arrivaler = LoginHelper.getNickname();
                    arrivalAddress = storeVo.getAddress();
                    sendContacts = warehouseVo.getName();
                    sendTel = warehouseVo.getTel();

                }

                break;
            case "4":
                break;
        }

        if (ObjectUtil.isNotNull(old)) {
            //交货人
//           vo.setOperatorName(userService.selectUserById(old.getOperator()).getNickName());

            if (old.getOperator() != null) {
                vo.setOperatorName(userService.selectUserById(old.getOperator()).getNickName());
            } else {
                vo.setOperatorName(taskVo.getName());
            }

            //接货人
            vo.setCounterpartName(userService.selectUserById(old.getCounterpart()).getNickName());

            //交接单
            SysOssBo oldBo = new SysOssBo();
            oldBo.setObjid(old.getObjid());
            oldBo.setBuss("handoverDtl");
            List<SysOss> oldList = ossService.selectSysOssList(oldBo);
            if (oldList != null && oldList.size() > 0) {
                vo.setOss(oldList);
            } else {
                vo.setOss(new ArrayList<>());
            }

        } else {
            //交货人
            if (vo.getOperator() != null) {
                vo.setOperatorName(userService.selectUserById(vo.getOperator()).getNickName());
            } else {
                vo.setOperatorName(taskVo.getName());
            }

            //接货人
            vo.setCounterpartName(userService.selectUserById(vo.getCounterpart()).getNickName());
            vo.setOss(new ArrayList<>());
        }


//        vo.setItems(dtls);

        /**
         * 交接清单共用参数
         * 移送单位 transferDeptName
         * 订单编号 taskNo
         * 取货时间 pickUpTime
         * 取货人 counterpartName
         * 取货地点 pickUpAddress
         * 到货时间 arrivalTime
         * 到货人 arrivaler
         * 到货地点 arrivalAddress
         * 联系人 sendContacts
         * 联系电话 sendTel
         */

        vo.setTransferDeptName(transferDeptName);
        vo.setTaskNo(taskNo);
        vo.setPickUpTime(pickUpTime);
        vo.setCounterpartName(counterpartName);
        vo.setPickUpAddress(pickUpAddress);
        vo.setArrivalTime(arrivalTime);
        vo.setArrivaler(arrivaler);
        vo.setArrivalAddress(arrivalAddress);
        vo.setSendContacts(sendContacts);
        vo.setSendTel(sendTel);
        vo.setTaskType(taskVo.getType());
        for (HandoverDtlVo handoverDtlVo : vo.getItems()) {
            handoverDtlVo.setDtlShow(false);
        }

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean handoverSave(HandoverBo bo) {

        boolean flag = false;

        HandoverVo vo = null;

        String content = null;
        String buss = null;
        Long deptId = null;
        String storeId = null;
        String orderType = null;
        /**
         * 车辆流转日志
         * 0-派单 1-出发 2-收件 3-取件交接 4-到达 5-签收
         */
        String logType = null;
        String waybillStatus = null;

        TaskBo taskBo = new TaskBo();
        OrderBo orderBo = new OrderBo();
        WaybillBo waybillBo = new WaybillBo();
        WarehouseBo warehouseBo = new WarehouseBo();
        ExWarehouseBo exWarehouseBo = new ExWarehouseBo();

        List<HandoverDtlVo> dtls = bo.getItems();//交接财物清单
        List<String> toUsers = new ArrayList<>();
        List<OrderVo> oldOrders = new ArrayList<>();
        /**
         * 交接操作类型
         * action
         * 0-取件 1-收件 2-出库签收 3-入库签收(到货) 4-暂存 5-暂存收件
         */

        TaskVo taskVo = taskService.queryById(bo.getObjid());
        BeanUtil.copyProperties(taskVo, taskBo);

        StoreVo storeVo = storeService.queryById(taskVo.getStoreId());

        OrderVo orderVo = orderService.queryByTaskId(bo.getObjid());
        BeanUtil.copyProperties(orderVo, orderBo);

        WaybillVo waybillVo = waybillService.queryByOrderId(orderBo.getId());
        BeanUtil.copyProperties(waybillVo, waybillBo);

        WarehouseVo warehouseVo = warehouseService.queryByTaskId(bo.getObjid());
        BeanUtil.copyProperties(warehouseVo, warehouseBo);

        ExWarehouseVo exWarehouseVo = exWarehouseService.queryByTaskId(bo.getObjid());
        BeanUtil.copyProperties(exWarehouseVo, exWarehouseBo);

        switch (bo.getAction()) {
            case "0":
                orderBo.setStatus("2");
                if (orderBo.getPickupId() == null) {
                    orderBo.setPickupId(LoginHelper.getUserId());
                    orderBo.setDriverName(LoginHelper.getNickname());
                }
                logType = "3";
                waybillStatus = "3";
                /**
                 * 设置取件时间
                 */
                waybillBo.setTakeTime(new Date());
                if (!StringUtils.isEmpty(waybillBo.getDriverName())) {
                    waybillBo.setDriverName(LoginHelper.getNickname());
                }
                content = "取件";
                buss = PropertyLogType.PICK_UP_HANDOVER.getCode();
                break;
            case "1":
                orderBo.setStatus("3");
                taskBo.setStatus("1");

                logType = "5";
                waybillStatus = "6";

                //处理入库单完成
                if (ObjectUtil.isNotNull(warehouseBo)) {
                    warehouseBo.setIsSign("1");
                }
                //处理出库单完成
                if (ObjectUtil.isNotNull(exWarehouseBo)) {
                    exWarehouseBo.setIsSign("1");
                }

                waybillBo.setStatus("6");
                waybillBo.setArriveTime(new Date());
                waybillBo.setSigner(LoginHelper.getNickname());
                waybillBo.setSignDeptId(LoginHelper.getDeptId());

                content = "签收";
                buss = PropertyLogType.SIGN_HANDOVER.getCode();

                /**
                 * 物品运输交接单绑定taskid
                 */
                bo.setTaskId(taskBo.getId());

                break;
            case "2":
                orderBo.setStatus("3");

                exWarehouseVo = exWarehouseService.queryByTaskId(bo.getTaskId());
                BeanUtil.copyProperties(exWarehouseVo, exWarehouseBo);
                exWarehouseBo.setIsSign("1");

                taskBo.setStatus("1");
                content = "签收";
                buss = PropertyLogType.EX_WAREHOUSE_HANDOVER.getCode();
                break;
            case "3":
                orderBo.setStatus("3");

                warehouseVo = warehouseService.queryByTaskId(bo.getTaskId());
                BeanUtil.copyProperties(warehouseVo, warehouseBo);
                warehouseBo.setIsSign("1");

                taskBo.setStatus("1");
                content = "签收";
                buss = PropertyLogType.ARRIVAL_HANDOVER.getCode();
                break;
            case "4":
                orderBo.setIsStaging("1");
                orderBo.setStagingTime(new Date());

                waybillBo.setStatus("6");
                waybillBo.setArriveTime(new Date());
                waybillBo.setSigner(LoginHelper.getNickname());
                waybillBo.setSignDeptId(LoginHelper.getDeptId());

                content = "暂存";
                buss = PropertyLogType.STAGING_HANDOVER.getCode();
                break;
            case "5":
                orderBo.setStatus("3");
                content = "签收";
                OrderBo old = new OrderBo();
                old.setFid(orderBo.getId());

                if (orderBo.getBuss().equals("1")) {
                    waybillBo.setStatus("5");
                } else {
                    waybillBo.setStatus("6");
                }
                waybillBo.setArriveTime(new Date());
                waybillBo.setSigner(LoginHelper.getNickname());
                waybillBo.setSignDeptId(LoginHelper.getDeptId());

                /**
                 * 查询暂存单之前旧的订单，并生成对应交接单
                 */
                oldOrders = orderService.queryList(old);
                buss = PropertyLogType.SIGN_HANDOVER.getCode();
                break;
        }


        if (!bo.getAction().equals("2") && !bo.getAction().equals("3")) {
            /**
             * 物流订单状态变更
             * 0-待派车 1-已派车，待取件 2-运输中 3-已完成
             */

            if (taskBo.getBuss().equals("1")) {
                orderBo.setCarStatus(logType);
                //车辆流转日志
                commonService.saveOrderLog(orderBo, logType);
            }

            orderService.updateByOrder(orderBo);
            commonService.orderPush(orderBo.getId());

            /**
             * 运单状态变更
             * 下发或者派遣时，运单状态默认为待发车状态
             * 非车辆流转：0-待接单 1-待发车 2-装车 3-发车 4-到站 5-派送 6-签收
             * 车辆流转：
             */
            if (taskBo.getBuss().equals("1")) {
                waybillBo.setStatus(waybillStatus);
            }
            waybillService.updateWaybillByBo(waybillBo);
        }

        /**
         * 入库单状态变更
         */
        if (ObjectUtil.isNotNull(warehouseBo) && StringUtils.isNotEmpty(warehouseBo.getId())) {
            warehouseService.updateByWarehouse(warehouseBo);
        }
        /**
         * 出库单状态变更
         */
        if (ObjectUtil.isNotNull(exWarehouseBo) & StringUtils.isNotEmpty(exWarehouseBo.getId())) {
            exWarehouseService.updateExWarehouseByBo(exWarehouseBo);

            if (exWarehouseBo.getType().equals("2")) {
                DealBo dealBo = new DealBo();
                dealBo.setTaskId(exWarehouseBo.getTaskId());
                List<DealVo> dealVos = dealService.queryList(dealBo);
                if (dealVos != null && dealVos.size() > 0) {
                    for (DealVo item : dealVos) {
                        item.setIsSign("1");//更新为已签收
                        DealBo updateBo = new DealBo();
                        BeanUtil.copyProperties(item, updateBo);
                        dealService.updateDealByBo(updateBo);

//                        DealDtlBo dealDtlBo = new DealDtlBo();
//                        dealDtlBo.setDealId(updateBo.getId());
//                        List<DealDtlVo> dealList = dealDtlService.queryList(dealDtlBo);
//                        if(dealList != null && dealList.size() > 0){
//                            for (DealDtlVo dtlVo:dealList){
//                                DealDtlBo dtlBo = new DealDtlBo();
//                                BeanUtil.copyProperties(dtlVo,dtlBo);
//                                dtlBo.setStatus("1");//更新为已签收
//                                dealDtlService.updateByBo(dtlBo);
//                            }
//                        }
                    }
                }
            }

        }


        /**
         * 处理交接信息
         */
        switch (bo.getAction()) {
            case "0":
                flag = handoverService.updateByBo(bo);

                //查询入库财物,没办法只能在这里加,下发就生成入库单
                WarehouseDtlBo whDtlBo = new WarehouseDtlBo();
                if(warehouseVo != null) {
                    whDtlBo.setWid(warehouseVo.getId());
                    List<WarehouseDtlVo> warehouseDtlVos = warehouseDtlService.queryList(whDtlBo);

                    dtls.stream().forEach(item -> {
                        //TODO 感觉改流程合理
                        WarehouseDtlVo warehouseDtlVo = warehouseDtlVos.stream()
                            .filter(warehouseDtl -> warehouseDtl.getPno().equals(item.getPno())).findFirst().get();

                        warehouseDtlVo.setUnit(item.getUnit());
                        warehouseDtlVo.setNums(item.getNums());
                        warehouseDtlService.updateByBo(BeanUtil.toBean(warehouseDtlVo, WarehouseDtlBo.class));

                        //更新交接财物数据量和单位
                        handoverDtlService.updateByBo(BeanUtil.toBean(item, HandoverDtlBo.class));
                        //更新财物是否鉴定
                        if (item.getEvaluate() != null && item.getEvaluate()) {
                            propertyService.updateProEvaluate(item.getPno());
                        }
                    });
                }

                commonService.saveOperatorLog(StringUtils.isNotEmpty(orderVo.getTaskId()) ? orderVo.getTaskId() : orderVo.getId(), "tms", content);
                //消息通知
                //消息接收人
                if (taskVo.getDeptId() != null) {
                    SysUser sysUser = userService.getDeptUser(taskVo.getDeptId());
                    if (sysUser != null && sysUser.getUserId() != null) {
                        toUsers.add(sysUser.getUserId().toString());
                    }
                }
                if (toUsers != null && toUsers.size() > 0) {
                    String contentInfo = "您的订单:" + taskVo.getTaskNo() + "已于" + DateUtils.date2String(new Date()) + "取件，请及时跟进！";
                    commonService.sendSysNotice("1", toUsers, deptId, "取件提醒", contentInfo, "{\"page\":\"/pagesOMS/staging/index\"}");
                }

                if (orderBo.getBuss().equals("1")) {
                    commonService.saveWaybillLog(waybillBo, "2");
                }

                if (storeVo != null) {

                    switch (taskBo.getType()) {
                        case "0":
                            orderType = "入库";
                            break;
                        case "1":
                            orderType = "出库";
                            break;
                        case "2":
                            orderType = "处置";
                            break;
                        case "3":
                            orderType = "调用";
                            break;
                    }

                    SmsRequest smsRequest = new SmsRequest();
                    smsRequest.setTemplateCode(SmsTemplateCode.WARE_HANDOVER_NOTICE.getSMS_CODE());
                    smsRequest.setPhoneNumber(userService.selectUserById(storeVo.getUserId()).getPhonenumber());
                    SmsTemplateParam templateParam = new SmsTemplateParam();
                    templateParam.setOrderType(orderType);
                    templateParam.setOrderCode(taskBo.getTaskNo());
                    smsRequest.setTemplateParam(templateParam);
                    aliyunSmsService.AliyunSmsSend(smsRequest);
                }


                break;
            case "5":
                flag = saveHandover(bo, oldOrders, content);
                //消息通知
                //消息接收人
                if (taskVo.getDeptId() != null) {
                    SysUser sysUser = userService.getDeptUser(taskVo.getDeptId());
                    if (sysUser != null && sysUser.getUserId() != null) {
                        toUsers.add(sysUser.getUserId().toString());
                    }
                }
                if (toUsers != null && toUsers.size() > 0) {
                    String contentInfo = "您的订单:" + taskVo.getTaskNo() + "已于" + DateUtils.date2String(new Date()) + "签收，请及时查阅！";
                    commonService.sendSysNotice("1", toUsers, deptId, "签收提醒", contentInfo, "{\"page\":\"/pagesOMS/staging/index\"}");
                }
                break;
            default:
                /**
                 * 订单状态变更
                 */
                taskService.finishTaskEnty(taskBo);
                HandoverVo updateVo = handoverService.updateByIdBo(bo);

                if (!bo.getAction().equals("2") && !bo.getAction().equals("3")) {
                    commonService.saveWaybillLog(waybillBo, "6");
//                    commonService.saveOrderLog(orderBo,"7");
//                    if(orderBo.getBuss().equals("1")){
//                        commonService.saveWaybillLog(waybillBo,"5");
//                    }else {
//                        commonService.saveWaybillLog(waybillBo,"6");
//                    }
                    commonService.saveOperatorLog(StringUtils.isNotEmpty(orderVo.getTaskId()) ? orderVo.getTaskId() : orderVo.getId(), "tms", content);
                } else {
                    commonService.saveOperatorLog(StringUtils.isNotEmpty(warehouseBo.getTaskId()) ? warehouseBo.getTaskId() : warehouseBo.getId(), "wms", content);
                }

                if (taskVo.getDeptId() != null) {
                    SysUser sysUser = userService.getDeptUser(taskVo.getDeptId());
                    if (sysUser != null && sysUser.getUserId() != null) {
                        toUsers.add(sysUser.getUserId().toString());
                    }
                }
                if (toUsers != null && toUsers.size() > 0) {
                    String contentInfo = "您的订单:" + taskVo.getTaskNo() + "已于" + DateUtils.date2String(new Date()) + "签收，请及时查阅！";
                    commonService.sendSysNotice("1", toUsers, deptId, "签收提醒", contentInfo, "{\"page\":\"/pagesOMS/staging/index\"}");
                }

                if (storeVo != null) {

                    switch (taskBo.getType()) {
                        case "0":
                            orderType = "入库";
                            break;
                        case "1":
                            orderType = "出库";
                            break;
                        case "2":
                            orderType = "处置";
                            break;
                        case "3":
                            orderType = "调用";
                            break;
                    }
                    if (updateVo.getStatus().equals("1")) {
                        SmsRequest smsRequest = new SmsRequest();
                        smsRequest.setTemplateCode(SmsTemplateCode.SIGN_NOTICE.getSMS_CODE());
                        smsRequest.setPhoneNumber(taskBo.getTel());
                        SmsTemplateParam templateParam = new SmsTemplateParam();
                        templateParam.setOrderType(orderType);
                        templateParam.setOrderCode(taskBo.getTaskNo());
                        smsRequest.setTemplateParam(templateParam);
                        aliyunSmsService.AliyunSmsSend(smsRequest);
                    }
                }

                break;
        }

        String finalBuss = buss;
        String finalContent = content;
        String info = JsonUtils.toJsonString(orderBo);
        deptId = orderBo.getDeptId();
        if (ObjectUtil.isNotNull(taskBo)) {
            storeId = taskBo.getStoreId();
        }
        String finalStoreId = storeId;
        Long finalDeptId = deptId;
        List<SysOss> ossList = new ArrayList<>();
        if (ObjectUtil.isNotNull(vo)) {
            SysOssBo ossBo = new SysOssBo();
            ossBo.setObjid(vo.getId());
            ossList = ossService.selectSysOssList(ossBo);
        }

        List<SysOss> finalOssList = ossList;
        dtls.stream().forEach(item -> {
            String logContent = finalContent + "：物品编号：" + item.getPno() + "，物品名称：" + item.getName();
            commonService.savePropertyLog(item.getPno(), bo.getObjid(), item.getName(), finalBuss, logContent, finalDeptId, info, finalStoreId, item.getNums(), finalOssList);
        });

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean dispatch(DispatchVo vo) {

        boolean flag = false;
        WaybillVo enty = null;

        OrderVo orderVo = orderService.queryById(vo.getId());
        OrderBo orderBo = new OrderBo();
        BeanUtil.copyProperties(orderVo, orderBo);

        TaskVo taskVo = taskService.queryById(orderBo.getTaskId());

        OrderDtlBo orderDtlBo = new OrderDtlBo();
        orderDtlBo.setOrderId(orderVo.getId());
        List<OrderDtlVo> dtls = orderDtlService.queryList(orderDtlBo);

        WaybillVo waybillVo = waybillService.queryByOrderId(orderBo.getId());
        WaybillBo waybillBo = new WaybillBo();
        BeanUtil.copyProperties(waybillVo, waybillBo);
        waybillBo.setNums(orderBo.getNums().intValue());

        orderBo.setCarNumber(vo.getCarNumber());//车牌号
        orderBo.setPickupId(vo.getPickupId());//取件人
        /**
         * 未修改订单下，默认派件人为当前取件人
         */
        orderBo.setDispatchId(vo.getPickupId());
        orderBo.setStatus("1");//待取件
        //设置车辆流转状态
        orderBo.setCarStatus("0");

        orderService.updateByOrder(orderBo);
        /**
         * 车辆流转日志
         * 0-派单 1-收件 2-取件交接 3-到达 4-签收
         */
        if (orderBo.getBuss().equals("1")) {
            commonService.saveOrderLog(orderBo, "0");
        }

        if (StringUtils.isEmpty(waybillBo.getId())) {
            waybillBo.setBuss(orderBo.getBuss());
            waybillBo.setOrderId(orderVo.getId());
            //车牌号
            waybillBo.setCarNumber(vo.getCarNumber());
            //司机
            waybillBo.setDriverName(userService.selectUserById(vo.getPickupId()).getNickName());
            //调度员
            waybillBo.setDispatcherId(orderBo.getDispatcherId());
            waybillBo.setDispatcher(userService.selectUserById(orderBo.getDispatcherId()).getNickName());
            //设置派单时间
            waybillBo.setDispatchTime(new Date());
            //设置接单时间
            waybillBo.setTakeBillTime(new Date());
            waybillBo.setBuss(orderVo.getBuss());
            //设置为待发车
            waybillBo.setStatus("2");
            //设置承运商
            try {
                CarrierVo carrierVo = carrierService.selectCarrierByDeptId(orderBo.getCarrierDeptId());
                waybillBo.setCarrierId(carrierVo.getId());
            } catch (Exception e) {
                System.out.println(e.getMessage());
                throw new ServiceException("当前部门存在多个承运商,请联系管理员处理！");
            }

            List<WaybillDtlVo> items = dtls.stream().map(item -> {
                WaybillDtlVo dtl = new WaybillDtlVo();
                BeanUtil.copyProperties(item, dtl, "id");
                return dtl;
            }).collect(Collectors.toList());

            waybillBo.setItems(items);


            //消息通知
            //消息接收人
            List<String> toUsers = new ArrayList<>();
            if (orderBo.getDeptId() != null) {
                //申请人
                SysUser sysUser = userService.getDeptUser(orderBo.getDeptId());
                if (sysUser != null && sysUser.getUserId() != null) {
                    toUsers.add(sysUser.getUserId().toString());
                }
            }
            //仓库
            if (StringUtils.isNotEmpty(taskVo.getStoreId())) {
                StoreVo storeVo = storeService.queryById(taskVo.getStoreId());
                SysUser storeUser = userService.getDeptUser(storeVo.getDeptId());
                if (storeUser != null && storeUser.getUserId() != null) {
                    toUsers.add(storeUser.getUserId().toString());
                }
            }
            /**
             * 消息推送
             */
            if (toUsers != null && toUsers.size() > 0) {
                String contentInfo = "您的订单:" + orderVo.getOrderNo() + "已于" + DateUtils.date2String(new Date()) + "派车，请及时跟进！";
                commonService.sendSysNotice("1", toUsers, orderBo.getDeptId(), "派车提醒", contentInfo, "{\"page\":\"/pagesOMS/staging/index\"}");
            }
            waybillBo.setLogContent("派单");
            waybillBo.setLogType(PropertyLogType.DISPATCH.getCode());

            enty = waybillService.insertByBo(waybillBo);

        } else {
            //车牌号
            waybillBo.setCarNumber(vo.getCarNumber());
            //司机
            waybillBo.setDriverName(userService.selectUserById(vo.getPickupId()).getNickName());
            //设置派单时间
            waybillBo.setDispatchTime(new Date());
            //设置接单时间
            waybillBo.setTakeBillTime(new Date());
            waybillBo.setLogContent("重新派单");
            waybillBo.setLogType(PropertyLogType.DISPATCH.getCode());
            enty = waybillService.resetDispatch(waybillBo);
        }

        commonService.orderPush(orderBo.getId());
        if (ObjectUtil.isNotNull(enty)) {
            flag = true;
        }
        return flag;
    }

    /**
     * 运单操作
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean waybillOperation(WaybillVo vo) {

        boolean flag = false;
        WaybillVo waybillVo = waybillService.queryById(vo.getId());
        WaybillBo bo = new WaybillBo();
        BeanUtil.copyProperties(waybillVo, bo);

        OrderVo orderVo = orderService.queryById(waybillVo.getOrderId());
        OrderBo orderBo = new OrderBo();
        BeanUtil.copyProperties(orderVo, orderBo);

        WaybillDtlBo dtlBo = new WaybillDtlBo();
        dtlBo.setWaybillId(bo.getId());
        List<WaybillDtlVo> dtls = waybillDtlService.queryList(dtlBo);

        String logType = null;
        String content = null;
        String contentInfo = null;
        String title = "提醒";
        String buss = PropertyLogType.TRANSPORT.getCode();

        WaybillVo wenty = null;

        /**
         * status
         * 2-发车 3-到站 4-派送
         */
        switch (bo.getStatus()) {
            case "2":
                logType = "2";
                content = "发车";
                bo.setStatus("3");
                bo.setSendTime(new Date());//发车时间
                orderBo.setDeliveryTime(new Date());//配送时间

                if (orderBo.getBuss().equals("1")) {
                    orderBo.setCarStatus("4");
                    commonService.saveOrderLog(orderBo, "4");//运输过程
                }

                title = "发车提醒";
                contentInfo = "您的订单:" + orderVo.getOrderNo() + "已于" + DateUtils.date2String(new Date()) + "发车，请及时跟进！";

                break;
            case "3":
                logType = "4";
                content = "到站";
                bo.setStatus("5");
                if (bo.getBuss().equals("1")) {
                    orderBo.setCarStatus("5");
                    commonService.saveOrderLog(orderBo, "5");//到达目的地
                }
                bo.setReachTime(new Date());//到站时间
                orderBo.setArriveTime(new Date());//送达时间

                title = "到站提醒";
                contentInfo = "您的订单:" + orderVo.getOrderNo() + "于" + DateUtils.date2String(new Date()) + "到站，请及时跟进！";
                break;
            case "4":
                logType = "5";
                content = "派送";
                bo.setStatus("5");
                bo.setDeliverTime(new Date());

                title = "派件提醒";
                contentInfo = "您的订单:" + orderVo.getOrderNo() + "于" + DateUtils.date2String(new Date()) + "正在派件中，请及时关注！";
                break;
            case "-1"://作废
                bo.setStatus("-1");
                bo.setCancelTime(new Date());//作废时间
                orderBo.setCancelTime(new Date());//作废时间
                break;
        }
        orderService.updateByOrder(orderBo);
        wenty = waybillService.updateWaybillByBo(bo);

        commonService.saveWaybillLog(bo, logType);

        if (StringUtils.isNotEmpty(orderBo.getTaskId())) {
            //操作日志
            commonService.saveOperatorLog(StringUtils.isNotEmpty(orderBo.getTaskId()) ? orderBo.getTaskId() : orderBo.getId(), "tms", content);
        } else {
            OrderBo queryBo = new OrderBo();
            queryBo.setFid(orderBo.getId());
            List<OrderVo> orderVoList = orderService.queryList(queryBo);
            if (orderVoList != null && orderVoList.size() > 0) {
                String finalContent = content;
                orderVoList.stream().forEach(item -> {
                    commonService.saveOperatorLog(StringUtils.isNotEmpty(item.getTaskId()) ? item.getTaskId() : item.getId(), "tms", finalContent);
                });
            }
        }
        String info = JsonUtils.toJsonString(orderVo);
        String finishContent = content;
        dtls.stream().forEach(item -> {
            commonService.savePropertyLog(item.getPno(), orderBo.getId(), item.getName(), buss, finishContent, orderBo.getDeptId(), info, null, orderBo.getNums(), null);
        });


        //消息通知
        //消息接收人
        List<String> toUsers = new ArrayList<>();
        if (orderVo.getDeptId() != null) {
            //申请人
            SysUser sysUser = userService.getDeptUser(orderVo.getDeptId());
            if (sysUser != null && sysUser.getUserId() != null) {
                toUsers.add(sysUser.getUserId().toString());
            }
        }
        //仓库
        TaskVo taskVo = taskService.queryById(orderBo.getTaskId());
        if (taskVo != null) {
            StoreVo storeVo = storeService.queryById(taskVo.getStoreId());
            if (storeVo != null) {
                SysUser storeUser = userService.getDeptUser(storeVo.getDeptId());
                if (storeUser != null && storeUser.getUserId() != null) {
                    toUsers.add(storeUser.getUserId().toString());
                }
            }
        }

        if (toUsers != null && toUsers.size() > 0) {
            try {
                commonService.sendSysNotice("1", toUsers, taskVo.getDeptId(), title, contentInfo, "{\"page\":\"/pagesOMS/staging/index\"}");
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        if (ObjectUtil.isNotNull(wenty)) {
            flag = true;
        }

        return flag;
    }

    /**
     * 车辆订单操作
     *
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean waybillOperationByCar(OrderVo vo) {

        boolean flag = false;
        OrderVo orderVo = orderService.queryById(vo.getId());
        OrderBo orderBo = new OrderBo();
        BeanUtil.copyProperties(orderVo, orderBo);

        WaybillVo waybillVo = waybillService.queryByOrderId(vo.getId());
        WaybillBo waybillBo = new WaybillBo();
        BeanUtil.copyProperties(waybillVo, waybillBo);

        String title = null;
        String contentInfo = null;

        String waybillStatus = null;
        String wLogType = null;
        /**
         * 车辆流转日志
         * 0-派单 1-出发 2-收件 3-取件交接 4-到达 5-签收
         */

        if (StringUtils.isNotEmpty(vo.getCarStatus())) {
            String logType = "";
            switch (vo.getCarStatus()) {
                case "1"://出发
                    logType = "1";
                    waybillStatus = "3";
                    break;
                case "2"://收件
                    logType = "2";
                    title = "收件提醒";
                    contentInfo = "取件员成功收件，请关注消息提醒及保持手机正常通讯！";
                    waybillStatus = "3";
                    break;
                case "4"://到达
                    logType = "4";
                    title = "到达提醒";
                    contentInfo = "派件员已到达目的地，请及时联系对方！";
                    waybillStatus = "5";
                    wLogType = "4";

                    //装车
//                    SysOssBo loadOss = new SysOssBo();
//                    loadOss.setObjid(vo.getId());
//                    loadOss.setBuss("load");
//                    List<SysOss> loadOssList = ossService.selectSysOssList(loadOss);
//                    if(loadOssList == null || loadOssList.size() == 0){
//                        throw new ServiceException("您有车辆附件未传,正在跳转到上传界面...");
//                    }
                    break;
                default:
                    break;
            }
            //多次联系客户时
            orderBo.setCarStatus(logType);

            flag = orderService.updateByOrder(orderBo);

            if (StringUtils.isNotEmpty(waybillStatus)) {
                waybillBo.setStatus(waybillStatus);
                waybillService.updateWaybillByBo(waybillBo);
            }

            //车辆流转日志
            commonService.saveOrderLog(orderBo, logType);
            //运单日志
            commonService.saveWaybillLog(waybillBo, wLogType);//到站
            if (!vo.getCarStatus().equals("1")) {
                //消息通知
                //消息接收人
                List<String> toUsers = new ArrayList<>();

                if (orderBo.getDeptId() != null) {
                    //申请人
                    SysUser sysUser = userService.getDeptUser(orderVo.getDeptId());
                    if (sysUser != null && sysUser.getUserId() != null) {
                        toUsers.add(sysUser.getUserId().toString());
                    }
                }
                //仓库
                TaskVo taskVo = taskService.queryById(orderBo.getTaskId());
                if (taskVo != null) {
                    StoreVo storeVo = storeService.queryById(taskVo.getStoreId());
                    if (storeVo != null) {
                        SysUser storeUser = userService.getDeptUser(storeVo.getDeptId());
                        if (storeUser != null && storeUser.getUserId() != null) {
                            toUsers.add(storeUser.getUserId().toString());
                        }
                    }
                }

                if (toUsers != null && toUsers.size() > 0) {
                    commonService.sendSysNotice("1", toUsers, taskVo.getDeptId(), title, contentInfo, "{\"page\":\"/pagesOMS/staging/index\"}");
                }
                /**
                 * 上门取件通知
                 */
                if (vo.getCarStatus().equals("1")) {
                    waybillService.updateWaybillByCarBo(waybillBo);
                }
            }
        }
        return flag;
    }

    /**
     * 暂存合并单签收
     *
     * @param bo
     * @param list
     * @param content
     * @return
     */
    public boolean saveHandover(HandoverBo bo, List<OrderVo> list, String content) {

        List<HandoverBo> bos = list.stream().map(item -> {

            OrderBo orderBo = new OrderBo();
            BeanUtil.copyProperties(item, orderBo);
            orderBo.setStatus("3");
            orderService.updateByBo(orderBo);

            TaskVo oldTask = taskService.queryById(item.getTaskId());
            TaskBo taskBo = new TaskBo();
            BeanUtil.copyProperties(oldTask, taskBo);
            taskBo.setStatus("1");//更新为已完成

            taskService.updateByBoV2(taskBo);

            WarehouseVo oldWarehouse = warehouseService.queryByTaskId(item.getTaskId());
            if (oldWarehouse != null) {
                WarehouseBo woldBo = new WarehouseBo();
                BeanUtil.copyProperties(oldWarehouse, woldBo);
                woldBo.setIsSign("1");//更新为已完成
                warehouseService.updateByBo(woldBo);
            }

            ExWarehouseVo oldExWarehouse = exWarehouseService.queryByTaskId(item.getTaskId());
            if (oldExWarehouse != null) {
                ExWarehouseBo eoldBo = new ExWarehouseBo();
                BeanUtil.copyProperties(oldExWarehouse, eoldBo);
                eoldBo.setIsSign("1");//更新为已完成
                exWarehouseService.updateByBo(eoldBo);
            }

            //获取取件信息
            HandoverBo oldBo = new HandoverBo();
            oldBo.setObjid(item.getTaskId());
            oldBo.setType("2");
            HandoverVo oldHand = handoverService.queryByObjid(oldBo);
            if (oldHand != null) {
                bo.setSigns(oldHand.getCounterpartSign());
            }
            //操作日志
            bo.setObjid(item.getTaskId());
            commonService.saveOperatorLog(StringUtils.isNotEmpty(item.getTaskId()) ? item.getTaskId() : item.getId(), "tms", content);
            return bo;
        }).collect(Collectors.toList());
        return handoverService.insertWithAllByBo(bos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchHandover(BatchHandover bo) {

        final boolean[] flag = {false};
        List<String> ids = new ArrayList<>();
        List<HandoverBo> bos = bo.getVos().stream().map(item -> {
            HandoverBo handoverBo = new HandoverBo();
            BeanUtil.copyProperties(item, handoverBo, "id");
            handoverBo.setCtpOssId(bo.getCtpOssId());
            handoverBo.setCheckOssId(bo.getCheckOssId());
            handoverBo.setCounterpartSign(bo.getCounterpartSign());
            handoverBo.setCheckSign(bo.getCheckSign());
            ids.add(item.getObjid());
            handoverBo.setAction("1");
            return handoverBo;
        }).collect(Collectors.toList());

        bos.stream().forEach(item -> {
            flag[0] = handoverSave(item);
        });
        return flag[0];
    }

    @Override
    public List<Map<String, Object>> getNoByList(String no, String type) {

        List<Map<String, Object>> results = new ArrayList<>();
        List<InventoryVo> ivos = null;
        List<WaybillDtlVo> wvos = null;
        List<PutDtlVo> pvos = null;
        InventoryBo ibo = new InventoryBo();
        WaybillDtlBo wbo = new WaybillDtlBo();
        PutDtlBo pbo = new PutDtlBo();
        /**
         * type: 0-物品明细 1-中转箱 2-保管箱 3-RFID
         */
        switch (type) {
            case "0":
//                ibo.setPno(no);
                ibo.setPnoKey(no);
                ivos = inventoryService.queryList(ibo);

                if (ivos != null && ivos.size() > 0) {
                    results = ivos.stream().map(item -> {
                        Map<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(item), Map.class);
                        return data;
                    }).collect(Collectors.toList());
                }

                break;
            case "1":
                wbo.setBoxNo(no);
                wvos = waybillDtlService.queryList(wbo);
                if (wvos != null && wvos.size() > 0) {
                    results = wvos.stream().map(item -> {
                        Map<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(item), Map.class);
                        return data;
                    }).collect(Collectors.toList());
                } else {
                    throw new ServiceException("查询不到订单信息！");
                }
                break;
            case "2":
                pbo.setBoxNo(no);
                pvos = putDtlService.queryList(pbo);

                if (pvos != null && pvos.size() > 0) {
                    results = pvos.stream().map(item -> {
                        Map<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(item), Map.class);
                        return data;
                    }).collect(Collectors.toList());
                }

                break;
            case "3":
                String prefix = "W3504";
                ivos = new ArrayList<>();
                List<String> pnos = Arrays.asList(no.split(","));
                if (pnos != null && pnos.size() > 0) {
                    for (String item : pnos) {
                        String pno = prefix + item;
//                        ibo.setPno(pno);
                        ibo.setPnoKey(pno);
                        ivos.addAll(inventoryService.queryList(ibo));
                    }
                }

                if (ivos != null && ivos.size() > 0) {
                    results = ivos.stream().map(item -> {
                        Map<String, Object> data = JSONObject.parseObject(JSONObject.toJSONString(item), Map.class);
                        return data;
                    }).collect(Collectors.toList());
                }
                break;
        }

        return results;
    }

    @Override
    public PropertyVo getPropertyInfo(String pno) {

        PropertyVo vo = propertyService.queryByNo(pno);
        return vo;
    }

    @Override
    public List<PropertyVo> getTaskDtls(List<String> ids) {

        List<PropertyVo> list = new ArrayList<>();
        if (ids != null && ids.size() > 0) {
            list = ids.stream().map(id -> {
                PropertyVo vo = propertyService.queryById(id);
                return vo;
            }).collect(Collectors.toList());
        }
        return list;
    }

}
